package com.lindenvalley.ch.ice;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class PreviewView extends View {
	
	private Bitmap mBitmapFrost, mBitmapStamp;
	private Matrix mMatrix;
	private Paint mPaint;
	private Canvas mCanvas;
	private float prevX, prevY, diffX, diffY;
	private boolean isFirstClick;
	private static int sRADIUS;
	private Context mContext;
	private float scaleValue, diffValue = 1;
	private long lastEventTime;
	private int maxRadius;
	private MediaPlayer dragPlayer;

	public PreviewView(Context context) {
		super(context);
		mContext = context;				
		_init();
	}

	public PreviewView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		_init();
	}
	
	public PreviewView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		_init();
	}
	
	public Bitmap getBitmap(){
		return mBitmapFrost;
	}
	
	public void initBitmap(){
		WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);		
		DisplayMetrics dm = new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(dm);
		BitmapDrawable drawable;
				
		if (dm.widthPixels > dm.heightPixels){
			drawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.frost_landscape);
			
		} else {
			drawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.frost_portrait);
		}
		
		BitmapDrawable bd = (BitmapDrawable) drawable.mutate();
		mBitmapFrost = bd.getBitmap();
		mBitmapFrost = Bitmap.createScaledBitmap(mBitmapFrost, dm.widthPixels, dm.heightPixels, true);
		mCanvas = new Canvas(mBitmapFrost);
	}
	
	private void _init(){
		
		initBitmap();		
		
		initStampPen();
		maxRadius = sRADIUS * 3;
		mMatrix = new Matrix();
		mPaint = new Paint();
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
		dragPlayer = MediaPlayer.create(mContext, R.raw.skrip);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas){		
		
		canvas.drawBitmap(mBitmapFrost, mMatrix, null);
		
	}
	
	private void initStampPen(){
		BitmapDrawable drawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.stamppen);		
		mBitmapStamp = drawable.getBitmap();
		sRADIUS = mBitmapStamp.getWidth()/2;
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			isFirstClick = true;			
		}
		if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP){
			try{
				dragPlayer.pause();
				dragPlayer.seekTo(0);
			} catch (IllegalStateException ex){
				ex.printStackTrace();
			}
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE){
//			if (dragPlayer.isPlaying() && dragPlayer.getCurrentPosition() < 2000){
//				dragPlayer.pause();
//				dragPlayer.seekTo(0);
//			}
			if (isFirstClick){
				prevX = x;
				prevY = y;
				isFirstClick = false;
				mMatrix.reset();
				scaleValue = 1;				
				lastEventTime = event.getEventTime();
				initStampPen();
				
			} else {
				scaleValue = 1.1f;
			}
			diffX = Math.abs(prevX - x);
			diffY = Math.abs(prevY - y);
			if (diffX <= sRADIUS*2 && diffY <= sRADIUS*2){
				long diffTime = event.getEventTime()-lastEventTime;
				if (event.getAction() == MotionEvent.ACTION_MOVE && diffX <= diffValue && diffY <= diffValue && diffTime > 400 && sRADIUS < maxRadius){
					int w = mBitmapStamp.getWidth();
					int h = mBitmapStamp.getHeight();
					mBitmapStamp = Bitmap.createScaledBitmap(mBitmapStamp, (int) (w*scaleValue), (int) (h*scaleValue), true);
					sRADIUS = mBitmapStamp.getWidth()/2;
					lastEventTime = event.getEventTime();
					if (dragPlayer.isPlaying())
						dragPlayer.pause();
				} 
				if (diffX > diffValue || diffY > diffValue){				
					initStampPen();
					lastEventTime = event.getEventTime();
				}				
				mCanvas.drawBitmap(mBitmapStamp, x - sRADIUS, y - sRADIUS, mPaint);
				if (!dragPlayer.isPlaying() && (diffX > diffValue || diffY > diffValue)){
					dragPlayer.seekTo(0);
					dragPlayer.start();
				} 
				
			}
			prevX = x;
			prevY = y;
		}
		invalidate();
		
		return true;
	}

}
